﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Configuration;
using Newtonsoft.Json;

namespace PromotionEngine
{
    public partial class PromotionDocumentPropertyInclusive : System.Web.UI.Page
    {
        public class ResponseDetail
        {
            public List<Model.PromotionDocumentPropertyInclude> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseDetail(List<Model.PromotionDocumentPropertyInclude> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseDetail(string errors)
            {
                this.Errors = errors;
            }

            public ResponseDetail()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string propertyID = Request.QueryString["PromotionID"];
            string propertyCode = Request.QueryString["PromotionCode"];
            string propertyName = Request.QueryString["PromotionName"];

            this.lblPromotionCode.Text = propertyCode;
            this.lblPromotionName.Text = propertyName;
            this.lblPromotionID.Value = propertyID;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetDocumentPropertyInclude(string promotionID, string propertyID)
        {
            List<Model.PromotionDocumentPropertyInclude> lstPromotionDocumentPropertyIn = new List<Model.PromotionDocumentPropertyInclude>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllPromotionDocumentPropertyInByPromotionID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID, promotionID);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.PromotionDocumentPropertyInclude promotionDocumentPropertyIn = new Model.PromotionDocumentPropertyInclude();
                    promotionDocumentPropertyIn.LineID = r["LineID"].ToString();
                    promotionDocumentPropertyIn.PromotionID = r["PromotionID"].ToString();
                    promotionDocumentPropertyIn.PropertyDetailID = r["PropertyDetailID"].ToString();
                    promotionDocumentPropertyIn.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    promotionDocumentPropertyIn.PropertyDetailName = r["PropertyDetailName"].ToString();
                    lstPromotionDocumentPropertyIn.Add(promotionDocumentPropertyIn);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstPromotionDocumentPropertyIn };
                return new ResponseDetail(lstPromotionDocumentPropertyIn, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocumentPropertyInclude(string oPromotionDocumentPropertyInclude, bool isInsert)
        {
            Model.PromotionDocumentPropertyInclude promotionDocumentProperty = JsonConvert.DeserializeObject<Model.PromotionDocumentPropertyInclude>(oPromotionDocumentPropertyInclude);
           
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonPromotionDocumentPropertyIn(promotionDocumentProperty.LineID, promotionDocumentProperty.PromotionID, promotionDocumentProperty.PropertyDetailID,
                                                    ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocumentPropertyIncludeList(string oPromotionDocumentPropertyInclude, bool isInsert)
        {
            List<Model.PromotionDocumentPropertyInclude> promotion = JsonConvert.DeserializeObject<List<Model.PromotionDocumentPropertyInclude>>(oPromotionDocumentPropertyInclude);

            Promotion pmt = new Promotion();
            foreach (Model.PromotionDocumentPropertyInclude item in promotion)
            {

                string errMsg = pmt.UpdateJsonPromotionDocumentPropertyIn(item.LineID, item.PromotionID, item.PropertyDetailID,
                                                        ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
                if (errMsg.Length == 0)
                {
                    //Show error message
                }
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteDocumentPropertyInclude(string lineID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonPromotionDocumentPropertyIn(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), lineID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
    }
}